//
// File:	GuiMenuOptions.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	29/01/2010 at 00:28:43
// Purpose:	
//

#include "Base.hpp"
#include "Config.hpp"
#include "GuiMenuOptions.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
GuiMenuOptions::GuiMenuOptions(QWidget *pParent /* = NULL */)
	:	GuiPanel(pParent)
{
	//Menu
	setObjectName("MenuOptions");

	//Title
	GuiLabel *pTitle = new GuiLabel(this);
	pTitle->move(100, 85);
	pTitle->resize(250, 55);
	pTitle->setText("OPTIONS");
	pTitle->setObjectName("Title");

	//Back
	_menuButtons[ButtonBack] = new GuiPushButton(this);
	_menuButtons[ButtonBack]->setText("Back");
	_menuButtons[ButtonBack]->setObjectName("ButtonBack");
	_menuButtons[ButtonBack]->resize(80, 25);
	_menuButtons[ButtonBack]->move(670, 550);

	//Group Box
	GuiGroupBox *pGroupBox = new GuiGroupBox(this);
	pGroupBox->move(100, 150);
	pGroupBox->resize(650, 390);

	//Player Nick Name
	_menuLabels[LabelPlayerNick] = new GuiLabel(pGroupBox);
	_menuLabels[LabelPlayerNick]->move(50, 50);
	_menuLabels[LabelPlayerNick]->resize(200, 20);
	_menuLabels[LabelPlayerNick]->setText("Player Nick Name:");
	_menuFields[FieldPlayerNick] = new GuiLineEdit(pGroupBox);
	_menuFields[FieldPlayerNick]->move(250, 50);
	_menuFields[FieldPlayerNick]->resize(300, 20);

	//Server Address
	_menuLabels[LabelServerAddress] = new GuiLabel(pGroupBox);
	_menuLabels[LabelServerAddress]->move(50, 100);
	_menuLabels[LabelServerAddress]->resize(200, 20);
	_menuLabels[LabelServerAddress]->setText("Server Address:");
	_menuFields[FieldServerAddress] = new GuiLineEdit(pGroupBox);
	_menuFields[FieldServerAddress]->move(250, 100);
	_menuFields[FieldServerAddress]->resize(300, 20);

	//Server Port
	_menuLabels[LabelServerPort] = new GuiLabel(pGroupBox);
	_menuLabels[LabelServerPort]->move(50, 150);
	_menuLabels[LabelServerPort]->resize(200, 20);
	_menuLabels[LabelServerPort]->setText("Server Port:");
	_menuFields[FieldServerPort] = new GuiLineEdit(pGroupBox);
	_menuFields[FieldServerPort]->move(250, 150);
	_menuFields[FieldServerPort]->resize(300, 20);

	//Fullscreen
	_menuLabels[LabelFullscreen] = new GuiLabel(pGroupBox);
	_menuLabels[LabelFullscreen]->move(50, 200);
	_menuLabels[LabelFullscreen]->resize(200, 20);
	_menuLabels[LabelFullscreen]->setText("Fullscreen:");
	_menuCheckboxes[CheckboxFullscreen] = new GuiCheckBox(pGroupBox);
	_menuCheckboxes[CheckboxFullscreen]->move(250, 200);
	_menuCheckboxes[CheckboxFullscreen]->resize(300, 20);

	//Slots
	QObject::connect(_menuButtons[ButtonBack], SIGNAL(clicked()), this, SLOT(onBackClicked()));
}

GuiMenuOptions::~GuiMenuOptions()
{
}

//------------------------------------------------------------------
// Events
//------------------------------------------------------------------
void GuiMenuOptions::showEvent(QShowEvent *e)
{
	//Read Settings
	_menuFields[FieldPlayerNick]->setText(QString::fromStdString(GlobalVariables::Config::Get().GetCvar("net_name")));
	_menuFields[FieldServerAddress]->setText(QString::fromStdString(GlobalVariables::Config::Get().GetCvar("net_server")));
	_menuFields[FieldServerPort]->setText(QString::fromStdString(GlobalVariables::Config::Get().GetCvar("net_port")));
	_menuCheckboxes[CheckboxFullscreen]->setChecked(GlobalVariables::Config::Get().GetCvar("r_fullscreen"));

	//Parent's Show Event
	GuiPanel::showEvent(e);
}

//------------------------------------------------------------------
// Slots
//------------------------------------------------------------------
void GuiMenuOptions::onBackClicked()
{
	//Save Settings
	GlobalVariables::Config::Get().SetCvar("net_name", _menuFields[FieldPlayerNick]->text().toStdString());
	GlobalVariables::Config::Get().SetCvar("net_server", _menuFields[FieldServerAddress]->text().toStdString());
	GlobalVariables::Config::Get().SetCvar("net_port", _menuFields[FieldServerPort]->text().toStdString());
	GlobalVariables::Config::Get().SetCvar("r_fullscreen", _menuCheckboxes[CheckboxFullscreen]->isChecked());
	GlobalVariables::Config::Get().WriteToFile(Config::GetConfigFilePath());

	//Apply settings
	GlobalVariables::DisplayManager::Get().GetWindow()->SetFullScreen(GlobalVariables::Config::Get().GetCvar("r_fullscreen"));
	
	//Show Main Menu
	GlobalVariables::GuiManager::Get().ShowPanel("menumain");
}
